package br.ufal.questione.dao.hibernate;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import br.ufal.questione.dao.DaoException;
import br.ufal.questione.dao.QuestionarioDao;
import br.ufal.questione.domain.Avaliador;
import br.ufal.questione.domain.Questionario;
import br.ufal.questione.domain.TipoQuestionario;

@Repository
public class HibernateQuestionarioDao extends HibernateGenericDao<Questionario, Long> implements QuestionarioDao {

	@SuppressWarnings("unchecked")
	@Override
	public List<Questionario> listar() {
		try {
			return getSession().createCriteria(getClassEntidade())
				.addOrder(Order.desc("dataFim"))
				.list();
		} catch (Exception e) {
			throw new DaoException(e);
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Questionario> listarDisponiveis(Avaliador avaliador) {
		try {
			List<TipoQuestionario> tiposQuestionario = new ArrayList<TipoQuestionario>();
			
			if (tiposQuestionario.isEmpty()) {
				return new ArrayList<Questionario>();
			}
			
			return getSession().createCriteria(getClassEntidade())
//				.add(Restrictions.in("tipoQuestionario", tiposQuestionario.toArray()))
				.add(Restrictions.le("dataInicio", new Date()))
				.add(Restrictions.gt("dataFim", new Date()))
				.addOrder(Order.asc("descricao"))
				.list();
		} catch (Exception e) {
			throw new DaoException(e);
		}
	}
	
}
